package cn.com.generaldata.os.service.indicator_evaluation.industry_structure_analysis;

import org.apache.commons.lang3.Validate;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.jsme_indicator_system.util.Constants;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 产业结构熵数评估指标计算类.
 */
public class IndustryStructureEntropyCalculateService extends IndicatorEvaluationCalculateService {

	private String addValueOfMajorMarineIndustryName = ProvinceIndicatorConstant.MAJOR_MARINE_ADDVALUE_NAME;

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {

		Double result = Double.valueOf(0);
		if (indicator.getSection() == Constants.INDICATOR_LEVEL_PROVINCE) {
			result = calculateResult(indicator, indicatorAccountVO);
		} else if (indicator.getSection() == Constants.INDICATOR_LEVEL_CITY) {
			result = calculateResult(indicator, indicatorAccountVO);
		}
		return result;

	}

	private Double calculateResult(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double result = 0d;
		Double provinceAddValueOfMajorMarineIndustry = getAddValueOfMajorMarineIndustry(indicatorAccountVO);
		// if (Double.valueOf("0").equals(provinceAddValueOfMajorMarineIndustry)) {
		// return Double.valueOf("0");
		// }
		Validate.isTrue(!(Double.valueOf(0).equals(provinceAddValueOfMajorMarineIndustry)));
		for (String industryName : ProvinceIndicatorConstant.MAJOR_MARINE_INDUSTRY_ADD_VALUE_MAP.values()) {
			Double addValueOfIndustry = getAddValueOfIndustry(indicatorAccountVO, industryName);
			if (addValueOfIndustry == null) {
				continue;
			}
			Double d = addValueOfIndustry / provinceAddValueOfMajorMarineIndustry;
			if (Double.valueOf(0).equals(d)) {
				continue;
			}
			result += d * Math.log(1 / d);
		}
		return result;
	}

	/**
	 * 获得十个主要海洋产业的增加值
	 * 
	 * @param indicatorAccountVO
	 * @param indicator
	 * @return
	 */
	private Double getAddValueOfIndustry(IndicatorAccountVO indicatorAccountVO, String industryName) {

		Double inputOrAccountingIndicatorValue = getInputOrAccountingIndicatorValue(indicatorAccountVO,
				indicatorAccountVO.getYear(),
				industryName);
		return inputOrAccountingIndicatorValue;
	}

	/**
	 * 获得主要海洋产业的增加值
	 * 
	 * @param indicatorAccountVO
	 * @param indicator
	 * @return
	 */
	private Double getAddValueOfMajorMarineIndustry(IndicatorAccountVO indicatorAccountVO) {
		Double inputOrAccountingIndicatorValue = getInputOrAccountingIndicatorValue(indicatorAccountVO,
				indicatorAccountVO.getYear(),
				addValueOfMajorMarineIndustryName);
		Validate.isTrue(inputOrAccountingIndicatorValue != null);
		return inputOrAccountingIndicatorValue;
	}

}
